package com.example.demo.controller;

import com.example.common.entity.ArticleInfo;
import com.example.common.entity.UserInfo;
import com.example.common.tools.AjaxResult;
import com.example.common.tools.UserSessionUtils;
import com.example.demo.service.ArticleService;
import com.example.demo.service.LikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import com.example.demo.entity.LikesInfo;

@RestController
@RequestMapping("/likes")
public class LikesController {

    @Autowired
    private LikesService likesService;

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/select")
    public AjaxResult selectLikes(HttpServletRequest request, Integer aid) {
        //非空校验
        if(aid == null) {
            return AjaxResult.fail(403, "参数错误1");
        }
        //查询当前用户信息
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
        !StringUtils.hasLength(userInfo.getPassword())) {
            return AjaxResult.fail(403, "参数错误2");
        }
        //先查询数据库中是否有该用户对此文章的点赞数据
        LikesInfo likesInfo = likesService.queryLikesInfoByUidAndAid(userInfo.getId(), aid);
        if(likesInfo == null) {
            //没有此点赞数据，那就加上此点赞数据，并设置为"已点赞"
            LikesInfo resLikeInfo = new LikesInfo(userInfo.getId(), aid, 1);
            //点赞量 +1
            articleService.updateLikesByAid(aid, articleService.getArtInfoById(aid).getLikes() + 1);
            return AjaxResult.success(likesService.add(resLikeInfo));
        } else {
            //有此点赞数据，那么只需要修改此状态设为 1 即可
            LikesInfo resLikeInfo =
                    new LikesInfo(userInfo.getId(), aid, 1);
            //点赞量 +1
            articleService.updateLikesByAid(aid, articleService.getArtInfoById(aid).getLikes() + 1);
            return AjaxResult.success(likesService.updateStateByLikesInfo(resLikeInfo));
        }
    }

    @RequestMapping("/cancel")
    public AjaxResult cancelLikes(HttpServletRequest request, Integer aid) {
        //非空校验
        if(aid == null) {
            return AjaxResult.fail(403, "aid为空！！！");
        }
        //查询当前登录用户信息
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
                !StringUtils.hasLength(userInfo.getPassword())) {
            return AjaxResult.fail(403, "参数错误2");
        }
        //修改当前点赞状态
        LikesInfo likesInfo =
                new LikesInfo(userInfo.getId(), aid, 0);
        //点赞量 -1
        articleService.updateLikesByAid(aid, articleService.getArtInfoById(aid).getLikes() - 1);
        return AjaxResult.success(likesService.updateStateByLikesInfo(likesInfo));
    }

    @RequestMapping("/count")
    public AjaxResult showInfo(HttpServletRequest request, Integer aid) {
        //非空校验
        if(aid == null) {
            return AjaxResult.fail(403, "参数错误1");
        }
        //通过博客 id 获取博客点赞数，进行检测
        ArticleInfo articleInfo = articleService.getArtInfoById(aid);
        if(articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle()) ||
        !StringUtils.hasLength(articleInfo.getContent())) {
            return AjaxResult.fail(403, "参数错误2");
        }
        //判断当前用户是否已经点赞过
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
        !StringUtils.hasLength(userInfo.getPassword())) {
            //未登录用户直接返回点赞数
            return AjaxResult.success(articleInfo.getLikes());
        }
        //已登录用户要显示两个信息:
        //1.是否点赞过。2.点赞数量
        LikesInfo likesInfo = likesService.queryLikesInfoByUidAndAid(userInfo.getId(), aid);
        if(likesInfo == null || likesInfo.getState() == 0) {
            //未点赞过
            return AjaxResult.success("no", articleInfo.getLikes());
        } else {
            return AjaxResult.success("yes", articleInfo.getLikes());
        }
    }

}
